#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
// map记录一下就可以了
int main() {
    unordered_map<string, vector<string> > stuMap;
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        vector<string> temp(3);
        cin >> temp[0] >> temp[1] >> temp[2];
        stuMap[temp[1]] = temp;
    } 
    cin >> n;
    string key;
    for (int i = 1; i <= n; i++) {
        cin >> key;
        vector<string> value;
        value = stuMap[key];
        cout << value[0] << " " << value[2] << endl;
    }
    
    return 0;
}